using System;
using System.Collections.Generic;
using System.Text;
using ECommerce.Library.DataStructure;
using Entity;

namespace AutoGear.BLL
{
    public class Events
    {
        #region Events Private Fields

        int _eventsId;
        string _eventsName = string.Empty;
        string _eventsBriefDescription = string.Empty;
        string _eventsDescription = string.Empty;
        string _eventsImageUrl = string.Empty;
        string _displayOrder = string.Empty;
        DateTime _eventsDate;
        bool _isActive;
        bool _isDeleted;
        DateTime _createDate;
        DateTime _updateDate;
        ReadWriteCustomCollection<Events> _relatedEvents;
        #endregion

        #region Events Constractor
        public Events() { }

        public Events
        (
            int eventsId,
            string eventsName,
            string eventsBriefDescription,
            string eventsDescription,
            string eventsImageUrl,
            DateTime eventsDate,
            string displayOrder,
            bool isActive,
            bool isDeleted,
            DateTime createDate,
            DateTime updateDate
        )
        {
            this._eventsId = eventsId;
            this._eventsName = eventsName;
            this._eventsBriefDescription = eventsBriefDescription;
            this._eventsDescription = eventsDescription;
            this._eventsImageUrl = eventsImageUrl;
            this._displayOrder = displayOrder;
            this._eventsDate = eventsDate;
            this._isActive = isActive;
            this._isDeleted = isDeleted;
            this._createDate = createDate;
            this._updateDate = updateDate;
        }
        public Events
       (
           string eventsName,
           string eventsBriefDescription,
           string eventsDescription,
           string eventsImageUrl,
           DateTime eventsDate,
           string displayOrder,
           bool isActive,
           bool isDeleted
       )
        {
            this._eventsName = eventsName;
            this._eventsBriefDescription = eventsBriefDescription;
            this._eventsDescription = eventsDescription;
            this._eventsImageUrl = eventsImageUrl;
            this._eventsDate = eventsDate;
            this._displayOrder = displayOrder;
            this._isActive = isActive;
            this._isDeleted = isDeleted;
        }
        #endregion

        #region Public Properties
        public int EventsId
        {
            get { return _eventsId; }
        }
        public string EventsName
        {
            get { return _eventsName; }
            set { _eventsName = value; }
        }
        public string EventsBriefDescription
        {
            get { return _eventsBriefDescription; }
            set { _eventsBriefDescription = value; }
        }
        public string EventsDescription
        {
            get { return _eventsDescription; }
            set { _eventsDescription = value; }
        }
        public string EventsImageUrl
        {
            get { return _eventsImageUrl; }
            set { _eventsImageUrl = value; }
        }
        public DateTime EventsDate
        {
            get { return this._eventsDate; }
            set { this._eventsDate = value; }
        }
        public string DisplayOrder
        {
            get { return _displayOrder; }
            set { _displayOrder = value; }
        }
        public bool IsActive
        {
            get { return this._isActive; }
            set { this._isActive = value; }
        }
        public bool IsDeleted
        {
            get { return this._isDeleted; }
            set { this._isDeleted = value; }
        }
        public DateTime CreateDate
        {
            get { return this._createDate; }
        }
        public DateTime UpdateDate
        {
            get { return this._updateDate; }
            set { this._updateDate = value; }
        }
        public ReadWriteCustomCollection<Events> RelatedEvents
        {
            get
            {
                _relatedEvents = new ReadWriteCustomCollection<Events>();
                ECommerce.DAL.CustomCollection<TblEventsRelated> related = TblEventsRelated.GetTblEventsRelatedByEventsId(this._eventsId);
                if (related != null)
                {
                    for (int i = 0; i < related.Count; i++)
                    {
                        try
                        {
                            Events events = Events.Load(related[i].RelatedEventsId);
                            if (events != null && events.IsActive)
                            {
                                _relatedEvents.Add(events);
                            }
                        }
                        catch { }
                    }
                }
                return this._relatedEvents;
            }
        }
        #endregion

        public void Create()
        {

            try
            {
                this._eventsId = TblEvents.CreateNewTblEvents(this._eventsName, this._eventsBriefDescription, this._eventsDescription, this._eventsImageUrl, this._eventsDate, this._displayOrder, this._isActive, this._isDeleted, DateTime.Now, DateTime.Now);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool Update()
        {
            try
            {
                TblEvents tblEvents = TblEvents.GetTblEventsByEventsId(this._eventsId);
                tblEvents.EventsName = this._eventsName;
                tblEvents.EventsBriefDescription = this._eventsBriefDescription;
                tblEvents.EventsDescription = this._eventsDescription;
                tblEvents.EventsImageUrl = this._eventsImageUrl;
                tblEvents.EventsDate = this._eventsDate;
                tblEvents.DisplayOrder = this._displayOrder;
                tblEvents.IsActive = this._isActive;
                tblEvents.IsDeleted = this._isDeleted;
                tblEvents.UpdateDate = DateTime.Now;
                return tblEvents.Save();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void Delete()
        {
            /**************************************************
             * will update database table, will delete physical
             * files and will write in admin log about the 
             * delete
             **************************************************/
            this._isDeleted = true;
            this.Update();
        }
        public void AddRelatedEvents(Events events)
        {
            if (events.EventsId == this.EventsId)
                return;
            ECommerce.DAL.CustomCollection<TblEventsRelated> relateds = TblEventsRelated.GetTblEventsRelatedByEventsId(this._eventsId);
            if (relateds != null)
            {
                for (int i = 0; i < relateds.Count; i++)
                {
                    if (events.EventsId == relateds[i].RelatedEventsId)
                    {
                        return;
                    }
                }
            }
            this.RelatedEvents.Add(events);
            TblEventsRelated related = new TblEventsRelated();
            TblEventsRelated.CreateNewTblEventsRelated(this._eventsId, events._eventsId);
        }

        public void DeleteRelatedEvents(int eventsId)
        {
            ECommerce.DAL.CustomCollection<TblEventsRelated> related = TblEventsRelated.GetTblEventsRelatedByEventsId(this._eventsId);
            if (related != null)
            {
                for (int i = 0; i < related.Count; i++)
                {
                    if (related[i].RelatedEventsId == eventsId)
                    {
                        TblEventsRelated.DeleteTblEventsRelated(related[i].TblEventsRelatedId);
                        break;
                    }
                }
            }
        }

        public static void DeleteEvents(ReadWriteCustomCollection<Events> Events)
        {
            foreach (Events events in Events)
            {
                events.Delete();
            }
        }

        public static ReadWriteCustomCollection<Events> GetEvents(int howMany)
        {
            try
            {
                ECommerce.DAL.CustomCollection<TblEvents> tblEvents = TblEventsExtended.GetActiveEventsByCount(howMany);
                ReadWriteCustomCollection<Events> Events = LoadFromCustomCollection(tblEvents);
                return Events;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Events Load(int eventsId)
        {
            try
            {
                TblEvents tmpTblEvents = TblEvents.GetTblEventsByEventsId(eventsId);
                Events events = new Events(tmpTblEvents.EventsId, tmpTblEvents.EventsName, tmpTblEvents.EventsBriefDescription, tmpTblEvents.EventsDescription, tmpTblEvents.EventsImageUrl, tmpTblEvents.EventsDate, tmpTblEvents.DisplayOrder, tmpTblEvents.IsActive, tmpTblEvents.IsDeleted, tmpTblEvents.CreateDate, tmpTblEvents.UpdateDate);
                return events;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static ReadWriteCustomCollection<Events> GetBackendEventsListByEventsName(string EventsName, bool includeDeleted, string orderBy, int startIndex, int pageSize)
        {
            if (includeDeleted)
            {
                return LoadFromCustomCollection(TblEventsExtended.GetAllEventsByEventsNamePaged(EventsName, orderBy, startIndex, pageSize));
            }
            else
            {
                return LoadFromCustomCollection(TblEventsExtended.GetNonDeletedEventsByEventsNamePaged(EventsName, orderBy, startIndex, pageSize));
            }
        }

        public static ReadWriteCustomCollection<Events> GetBackendEventsList(bool includeDeleted, string orderBy, int startIndex, int pageSize)
        {
            if (includeDeleted)
            {
                return LoadFromCustomCollection(TblEvents.GetAllTblEventsPaged(orderBy, startIndex, pageSize));
            }
            else
            {
                return LoadFromCustomCollection(TblEventsExtended.GetNonDeletedEventsPaged(orderBy, startIndex, pageSize));
            }
        }

        public static int GetBackendEventsListCount(bool includeDeleted)
        {
            if (includeDeleted)
            {
                return TblEvents.GetAllTblEventsPagedCount("", 0, 0);
            }
            else
            {
                return TblEventsExtended.GetNonDeletedEventsCount();
            }
        }

        public static int GetBackendEventsListByEventsNameCount(string EventsName, bool includeDeleted)
        {
            if (includeDeleted)
            {
                return TblEventsExtended.GetAllEventsByEventsNameCount(EventsName);
            }
            else
            {
                return TblEventsExtended.GetNonDeletedEventsByEventsNameCount(EventsName);
            }
        }

        private static ReadWriteCustomCollection<Events> LoadFromCustomCollection(ECommerce.DAL.CustomCollection<TblEvents> tblEvents)
        {
            ReadWriteCustomCollection<Events> result = new ReadWriteCustomCollection<Events>();
            for (int i = 0; i < tblEvents.Count; i++)
            {
                TblEvents tmpTblEvents = tblEvents[i];
                result.Add(new Events(tmpTblEvents.EventsId, tmpTblEvents.EventsName, tmpTblEvents.EventsBriefDescription, tmpTblEvents.EventsDescription, tmpTblEvents.EventsImageUrl, tmpTblEvents.EventsDate, tmpTblEvents.DisplayOrder, tmpTblEvents.IsActive, tmpTblEvents.IsDeleted, tmpTblEvents.CreateDate, tmpTblEvents.UpdateDate));
            }
            return result;
        }


    }
}
